package cn.liweixi.log.controller;

import cn.liweixi.basic.utils.PageList;
import cn.liweixi.basic.utils.Result;
import cn.liweixi.log.aspect.Operation;
import cn.liweixi.log.domain.Log;
import cn.liweixi.log.service.ILogService;
import cn.liweixi.org.query.DepartmentQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "日志操作的API",description="操作相关的功能")
@RequestMapping("/log")
@RestController

public class TableController {
    @Autowired
    private ILogService logService;

    //查询所有
    @Operation(value = "查询用户")
    @GetMapping
    public List<Log> findAll(){
        return logService.findAll();
    }

    //根据id查询数据
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @Operation
    public Log findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return logService.findById(id);
    }

    //修改或添加
    @PutMapping
    @Operation(value = "修改或添加" )
    public Result addOrUpdate(@RequestBody Log log) {
        try {
            if (null == log.getLogId()){
                //添加数据
                logService.add(log);
            }else{
                //修改数据
                logService.updateById(log);
            }
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    //删除数据
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工" )
    @Operation(value = "删除数据" )
    public Result delete(@PathVariable("id") Long id){
        try {
            logService.delete(id);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }

    //分页查询
    @PostMapping
    @Operation(value = "刷新" )
    public PageList<Log> queryByPage(@RequestBody DepartmentQuery query){
        return logService.queryByPage(query);
    }

    //批量删除
    @PatchMapping
    @Operation(value = "批量删除" )
    public Result batchDel(@RequestBody List<Long> ids){
        try {
            logService.batchDel(ids);
            return Result.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.me().fail();
    }
}
